<?php

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/common/SearchObject.php';
require_once ROOT_PATH . '/lib/models/surveys/Options.php';

class Questions {

	const TABLE_NAME = 'hs_hr_sur_questions';
	
	const DB_FIELD_QUESTIONS_ID = 'question_id';
	const DB_FIELD_QUESTTIONS_TEXT = 'question_text';
	const DB_FIELD_QUESTTIONS_SURVEY_ID = 'survey_id';
	const DB_FIELD_QUESTTIONS_ANSWER_TYPE = 'answer_type';
	
	private $questions_id;
	private $question_text;
	private $survey_id;
	private $answer_type;
	
	public function __construct($questions_id = null) {
		$this->questions_id = $questions_id;
	}

	public function setId($id) {
		$this->questions_id = $id;
	}

	public function setText($question_text) {
		$this->question_text = $question_text;
	}
	
	public function setSurveyId($survey_id) {
		$this->survey_id = $survey_id;
	}
	
	public function setAnswerType($answer_type) {
		$this->answer_type = $answer_type;
	}
	
	public function getId() {
		return $this->questions_id;
	}

	public function getText() {
		return $this->question_text;
	}

	public function getSurveyId() {
		return $this->survey_id;
	}
	
	public function getAnswerType() {
		return $this->answer_type;
	}

    public function save() {
		if (isset($this->questions_id)) {
			return $this->_update();
		} else {
			return $this->_insert();
		}
    }

	public static function delete($ids) {

		$count = 0;
		if (!is_array($ids)) {
			throw new QuestionsException("Invalid parameter to delete(): ids should be an array", QuestionsException::INVALID_PARAMETER);
		}

		foreach ($ids as $id) {
			if (!CommonFunctions::isValidId($id)) {
				throw new QuestionsException("Invalid parameter to delete(): id = $id", QuestionsException::INVALID_PARAMETER);
			}
		}

		if (!empty($ids)) {

			$sql = sprintf("DELETE FROM %s WHERE %s IN (%s)", self::TABLE_NAME,
			                self::DB_FIELD_QUESTIONS_ID, implode(",", $ids));

			$conn = new DMLFunctions();
			$result = $conn->executeQuery($sql);
			if ($result) {
				$count = mysql_affected_rows();
			}
		}
		return $count;
	}


	public static function getMaxId() {
		$sql = sprintf("SELECT MAX(question_id) AS NEW_ID FROM %s ", self::TABLE_NAME);
		
			$conn = new DMLFunctions();
			$result = $conn->executeQuery($sql);
			 $max_venue = mysql_result($result,0,0);  
		
		return $max_venue;
	}
	
	public static function getAll() {
		return self::_getList();
	}
	
	public static function getQuestionsBySurvey($surveyId) {
		$condition[0]= "(".self::DB_FIELD_QUESTTIONS_SURVEY_ID." = ".$surveyId .")";
		return self::_getList($condition);
	}
	
	public static function getQuestionsExist($qestionText,$surveyId) {
		$condition[0]= "(".self::DB_FIELD_QUESTTIONS_SURVEY_ID." = ".$surveyId .")";
		$condition[1]= "(".self::DB_FIELD_QUESTTIONS_TEXT." = '".$qestionText ."')";
		return self::_getList($condition);
	}
	
	public static function getCustomQuestionsBySurvey($surveyId) {
		$condition[0]= "(".self::DB_FIELD_QUESTTIONS_SURVEY_ID." = ".$surveyId .")";
		$condition[1]= "(".self::DB_FIELD_QUESTTIONS_ANSWER_TYPE." = 4)";
		return self::_getList($condition);
	}
	
	public static function getQuestionById($id) {
		$condition[0]= "(".self::DB_FIELD_QUESTIONS_ID." = ".$id .")";
		$results = self::_getList($condition); 		
		return $results[0];
	}
	
	public static function getOptions($question_id) {	
		$options = Options::getOptionsByQuestions($question_id); 
		return $options;
	}
	
	public static function getMaxID2() {	
		$questions = self::_getList();
		$maxId = 1;
		if(count($questions)>0){
			foreach($questions as $question){
				if($question->getId()> $maxId){
					$maxId = $question->getId();
				}
			}
		}
		return $maxId;
	}

	private static function _getList($selectCondition = null) {
		$fields[0] = self::DB_FIELD_QUESTIONS_ID;
		$fields[1] = self::DB_FIELD_QUESTTIONS_TEXT;
		$fields[2] = self::DB_FIELD_QUESTTIONS_SURVEY_ID;
		$fields[3] = self::DB_FIELD_QUESTTIONS_ANSWER_TYPE;
		
		$tables[0] = self::TABLE_NAME ;
		
		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition);
		//die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		while ($result && ($row = mysql_fetch_assoc($result))) {
			$actList[] = self::_createFromRow($row);
		}

		return $actList;
	}

	private function _insert() {
				
		//$fields[0] = self::DB_FIELD_QUESTIONS_ID;
		$fields[0] = self::DB_FIELD_QUESTTIONS_TEXT;
		$fields[1] = self::DB_FIELD_QUESTTIONS_SURVEY_ID;
		$fields[2] = self::DB_FIELD_QUESTTIONS_ANSWER_TYPE;		

		//$values[0] = $this->questions_id;
		$values[0] = "'{$this->question_text}'";
		$values[1] = $this->survey_id;
		$values[2] = $this->answer_type;
		
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$conn = new DMLFunctions();

		$result = $conn->executeQuery($sql);
		if (!$result || (mysql_affected_rows() != 1)) {
			throw new QuestionsException("Insert failed. ", QuestionsException::DB_ERROR);
		}

		return $this->questions_id;
	}

	private function _update() {

		$fields[0] = self::DB_FIELD_QUESTIONS_ID;
		$fields[1] = self::DB_FIELD_QUESTTIONS_TEXT;
		$fields[2] = self::DB_FIELD_QUESTTIONS_SURVEY_ID;
		$fields[3] = self::DB_FIELD_QUESTTIONS_ANSWER_TYPE;
		
		$values[0] = $this->questions_id;
		$values[1] = "'{$this->question_text}'";
		$values[2] = $this->survey_id;		
		$values[3] = $this->answer_type;

		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_update = 'true';
		$sqlBuilder->arr_update = $fields;
		$sqlBuilder->arr_updateRecList = $values;

		$sql = $sqlBuilder->addUpdateRecord1(0);

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		if (!$result) {
			throw new QuestionsException("Update failed. SQL=$sql", QuestionsException::DB_ERROR);
		}
		return $this->questions_id;
	}

    private static function _createFromRow($row) {
    	
    	$question = new Questions($row[self::DB_FIELD_QUESTIONS_ID]);
    	$question->setText($row[self::DB_FIELD_QUESTTIONS_TEXT]);    	
    	$question->setSurveyId($row[self::DB_FIELD_QUESTTIONS_SURVEY_ID]);
    	$question->setAnswerType($row[self::DB_FIELD_QUESTTIONS_ANSWER_TYPE]);
    	return $question;
    }

}

class QuestionsException extends Exception {
	const INVALID_PARAMETER = 0;
	const DB_ERROR = 1;
}

?>